﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using UFPC.Models;

namespace UFPC.Controllers
{
    [Authorize(Roles="Administrator")]
    public class InventoryController : Controller
    {
        private IUfpcRepository _repository;

        public InventoryController() : this(new UfpcRepository())
        { 
            
        }

        public InventoryController(IUfpcRepository repository)
        {
            _repository = repository;
        }
        
        //
        // GET: /Inventory/

        public ActionResult Index()
        {
            return View(_repository.ListInventories());
        }

        //
        // GET: /Inventory/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Inventory/Create

        public ActionResult Create()
        {
            Inventory inventory = new Inventory();
            
            return View(new InventoryFormViewModel(inventory, _repository.ListProducts()));
        } 

        //
        // POST: /Inventory/Create

        [HttpPost]
        public ActionResult Create([Bind(Exclude="InventoryId")]Inventory newInventory)
        {
            if (ModelState.IsValid)
            {
                _repository.AddInventory(newInventory);
                _repository.Save();

                return RedirectToAction("Index");
            }

            return View();
        }
        
        //
        // GET: /Inventory/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Inventory/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Inventory/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Inventory/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
